﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataSet1TableAdapters;

public partial class photo : System.Web.UI.Page
{
    public string[] FotoUrl = new string[10]; // + ŠITO kodu
    public string[] Virsraksts1 = new string[10]; // virsraksti
    int PageElementNumber = 10;
    RakstiTableAdapter RakstiTa = new RakstiTableAdapter();
    DataSet1.RakstiDataTable RakstiDa = new DataSet1.RakstiDataTable();

    DisplayTableAdapter DisplayTa = new DisplayTableAdapter();
    DataSet1.DisplayDataTable DispayDs = new DataSet1.DisplayDataTable();

    SadalaTableAdapter SadalaTa = new SadalaTableAdapter();
    DataSet1.SadalaDataTable SadalaDs = new DataSet1.SadalaDataTable();

    DataSet1.RakstiDataTable RakstiAll = new DataSet1.RakstiDataTable();
    int CurrentPage = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        RakstiDa = RakstiTa.GetDataBySortDatums(); // ir jau sasortēti pēc datuma. 
        foreach (DataSet1.RakstiRow Rr in RakstiDa)
        {
            DispayDs = DisplayTa.GetDataByRakstID(Rr.ID_name);
            bool Ir = false;
            if (DispayDs.Rows.Count > 0)
            {
                foreach (DataSet1.DisplayRow Dr in DispayDs)
                {
                    if ((Dr.SadalasID == 2))
                    {
                        Ir = true;
                        break;
                    }
                }
            }
            if (Ir)
            {
                RakstiAll.Rows.Add(Rr.ItemArray);
            }
        }
        int pageCCOunt = RakstiAll.Count / PageElementNumber;
        int InvertedPage = 0;
        string html_txt = "";
        try // IR tā Ka Page Nummuri ir Invertēti, proti 0 lapa ir pēdējā mājaslapas Texta, un otādies, un 
        { // šādi tad to Realizē. jeb es esu uzlabojis esošo sistēmu kur 0 laba bīj 0 lapa arī kodā, bet tākā 
            // Negribās neko Fundamentāli mainīt, esu veicis, uzlabojumus,apgriezis nummerāciju , nemainot pārējo kodu :).
            html_txt = String.Format(Request.QueryString["Page"]);
            InvertedPage = Convert.ToInt32(html_txt);
        }
        catch (ArgumentNullException anull)
        {
            InvertedPage = pageCCOunt;
        }
        CurrentPage = pageCCOunt - InvertedPage; // Invertē Lapas Nummerāciju, tas priekš GoggleBota.

        // CreateDataTableVideo(); // izveido DataTable
        HyperLink[] PoguList = new HyperLink[9];
        PoguList[0] = HyperLink_first;
        PoguList[1] = HyperLink_mi10;
        PoguList[2] = HyperLink_mi4;
        PoguList[3] = HyperLink_mi1;
        PoguList[4] = HyperLink_curent;
        PoguList[5] = HyperLink_plu1;
        PoguList[6] = HyperLink_plu4;
        PoguList[7] = HyperLink_plu10;
        PoguList[8] = HyperLink_Last;
        PageNumberButtons("photo.aspx?Page=", PoguList, pageCCOunt, InvertedPage);

        ItemsGet();
    }

    private void ItemsGet()
    {
        Array.Resize(ref FotoUrl, 10);
        Array.Resize(ref Virsraksts1, 10);
        int ItemMax = 0;
        if (RakstiAll.Count < CurrentPage * PageElementNumber + PageElementNumber)
        {
            ItemMax = RakstiAll.Count - CurrentPage * PageElementNumber;
        }
        else
        {
            ItemMax = 10;
        }

        for (int i = 0; i < ItemMax; i++)
        {
            FotoUrl[i] = RakstiAll[i + CurrentPage * PageElementNumber].bildeTumb;
            Virsraksts1[i] = RakstiAll[i + CurrentPage * PageElementNumber].Virsraksts;
        }
        Page.DataBind();
        // paging kods ko iekopē
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = RakstiAll.DefaultView;// Tables[0].DefaultView;
        // Indicate that the data should be paged
        objPds.AllowPaging = true;
        // Set the number of items you wish to display per page
        objPds.PageSize = PageElementNumber;
        // Set the PagedDataSource's current page
        objPds.CurrentPageIndex = CurrentPage;// -1;
        lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
        + objPds.PageCount.ToString();

        Repeater1.DataSource = objPds;
        Repeater1.DataBind();
    }

    void PageNumberButtons(string html_url, HyperLink[] Pogas, int PageCount, int Inverted_Page)
    {
        int currentPage = PageCount - Inverted_Page;
        if (Pogas.Length == 9)
        {
            if (PageCount > 10 || currentPage - 1 > 0 || currentPage - 4 > 0)
            {
                // HyperLink_first.Enabled = true;
                Pogas[0].Visible = true;
                Pogas[0].Text = "1";
                Pogas[0].NavigateUrl = html_url + PageCount.ToString();// +"0";
            }
            else { Pogas[0].Visible = false; }
            if (currentPage - 10 >= 0)
            {
                Pogas[1].Visible = true;
                Pogas[1].Text = (currentPage - 10 + 1).ToString();
                Pogas[1].NavigateUrl = html_url + (Inverted_Page + 10).ToString(); // (currentPage - 10).ToString();
            }
            else { Pogas[1].Visible = false; }
            if (currentPage - 4 >= 0)
            {
                Pogas[2].Visible = true;
                Pogas[2].Text = (currentPage - 4 + 1).ToString();
                Pogas[2].NavigateUrl = html_url + (Inverted_Page + 4).ToString(); //+ (currentPage - 4).ToString();
            }
            else { Pogas[2].Visible = false; }
            if (currentPage - 1 >= 0)
            {
                Pogas[3].Visible = true;
                Pogas[3].Text = (currentPage - 1 + 1).ToString();
                Pogas[3].NavigateUrl = html_url + (Inverted_Page + 1).ToString(); //+ (currentPage - 1).ToString();
            }
            else { Pogas[3].Visible = false; }

            Pogas[4].Text = (currentPage + 1).ToString();
            Pogas[4].NavigateUrl = html_url + Inverted_Page; // currentPage;
            if (currentPage + 1 <= PageCount)
            {
                Pogas[5].Visible = true;
                Pogas[5].Text = (currentPage + 1 + 1).ToString();
                Pogas[5].NavigateUrl = html_url + (Inverted_Page - 1).ToString(); //(currentPage + 1).ToString();
            }
            else { Pogas[5].Visible = false; }
            if (currentPage + 4 <= PageCount)
            {
                Pogas[6].Visible = true;
                Pogas[6].Text = (currentPage + 4 + 1).ToString();
                Pogas[6].NavigateUrl = html_url + (Inverted_Page - 4).ToString(); // +(currentPage + 4).ToString();
            }
            else { Pogas[6].Visible = false; }
            if (currentPage + 10 <= PageCount)
            {
                Pogas[7].Visible = true;
                Pogas[7].Text = (currentPage + 10 + 1).ToString();
                Pogas[7].NavigateUrl = html_url + (Inverted_Page - 10).ToString(); //+ (currentPage + 10).ToString();
            }
            else { Pogas[7].Visible = false; }
            if (PageCount >= currentPage + 10 || currentPage + 1 < PageCount || currentPage + 4 < PageCount)
            {
                Pogas[8].Visible = true;
                Pogas[8].Text = (PageCount + 1).ToString();
                Pogas[8].NavigateUrl = html_url + "0"; // +PageCount.ToString();
            }
            else { Pogas[8].Visible = false; }
        }
    }

    // ŠOTAS ir JAUNAIS Kods
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            int Nr = e.Item.ItemIndex;
            if (Nr % 2 == 0) //then it's even
            {
                if (FotoUrl[Nr] != "")
                {
                    HyperLink bildeLnk = ((HyperLink)e.Item.FindControl("BildeLink1"));
                    bildeLnk.Text = "<img src='" + FotoUrl[Nr] + "' style='width:100px'/> ";
                    bildeLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
                }
                HyperLink TitleLnk = ((HyperLink)e.Item.FindControl("TitleLink"));
                TitleLnk.Text = Virsraksts1[Nr];
                TitleLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
            }
            else
            { //It is odd
                if (FotoUrl[Nr] != "")
                {
                    HyperLink bildeLnk = ((HyperLink)e.Item.FindControl("BildeLink2"));
                    bildeLnk.Text = "<img src='" + FotoUrl[Nr] + "' style='width:100px'/> ";
                    bildeLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
                }
                HyperLink TitleLnk = ((HyperLink)e.Item.FindControl("TitleLink"));
                TitleLnk.Text = Virsraksts1[Nr];
                TitleLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
            }
        }
        else if (e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int Nr = e.Item.ItemIndex;
            if (Nr % 2 == 0) //then it's even
            {
                if (FotoUrl[Nr] != "")
                {
                    HyperLink bildeLnk = ((HyperLink)e.Item.FindControl("BildeLink1"));
                    bildeLnk.Text = "<img src='" + FotoUrl[Nr] + "' style='width:100px'/> ";
                    bildeLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
                }
                HyperLink TitleLnk = ((HyperLink)e.Item.FindControl("TitleLink"));
                TitleLnk.Text = Virsraksts1[Nr];
                TitleLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
            }
            else
            { //It is odd
                if (FotoUrl[Nr] != "")
                {
                    HyperLink bildeLnk = ((HyperLink)e.Item.FindControl("BildeLink2"));
                    bildeLnk.Text = "<img src='" + FotoUrl[Nr] + "' style='width:100px'/> ";
                    bildeLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
                }
                HyperLink TitleLnk = ((HyperLink)e.Item.FindControl("TitleLink"));
                TitleLnk.Text = Virsraksts1[Nr];
                TitleLnk.NavigateUrl = "DefaultZoom.aspx?value=" + TitleLink(Virsraksts1[Nr]);
            }
        }
    }
    protected string TitleLink(string Vr)
    {
        char[] SpaceDalitajs = { ' ' };

        string Vr_new = "";
        string Vr_line = "";
        if (Vr.Contains("-") == true)
        {
            string[] Paa = Vr.Split('-');
            Vr_line = Paa[0];
            for (int Pab = 1; Pab < Paa.Length; Pab++)
            {
                Vr_line += "--" + Paa[Pab];
            }
        }
        else { Vr_line = Vr; }
        string[] Vr_sadalits = Vr_line.Split(SpaceDalitajs);
        if (Vr_sadalits.Length != 1)
        {
            Vr_new = Vr_sadalits[0];
            for (int ii = 1; ii < Vr_sadalits.Length; ii++)
            {
                Vr_new += "-" + Vr_sadalits[ii];
            }
        }
        else { Vr_new = Vr_sadalits[0]; }
        return Vr_new;
    }

}
